﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xiami.util;
using System.IO;
using System.Text.RegularExpressions;

namespace Xiami.lib {
    public class XFile {
        
        protected string _filePath; //文件
        protected string[] _fileFormats;

        protected Hashtable _paramPool; 
        protected Error _error;
        protected XXiami _form;

        protected static string[] needle = new string [] {",", "，"};

        protected static string defDir = @"C:\Documents and Settings\{0}\Local Settings\Temporary Internet Files\Content.IE5";

        public XFile() {
            this._filePath = "";
            this._paramPool = new Hashtable();
            this._fileFormats = new string [] {"*"};
            this._form = null;
            this._error = new Error();
        }

        public Boolean copyFile() {
            if(false == this.verifySrcDir()) {
                return false;
            }
            this.verifyDesDir();
            this.copy(this.getSrcDir());

            return true;
        }

        protected Boolean verifySrcDir() {
            if(false == XVerify.isDir(this.getSrcDir())) {
                this._error.setErrorInfo("源目录不存在！" + this.getSrcDir());
                return false;
            }

            return true;
        }

        protected string genIeTempDirPath(string userName) {
            return String.Format(XFile.defDir, userName);
        }

        protected Boolean verifyDesDir() {
            if(XVerify.isDir(this.getDesDir())) {
                return true;
            }
            this.setParamPoolElement("desDir", this.newDir(this.getDesDir()));
            return true;
        }

        protected string newDir(string dirPath) {
            if(false == XVerify.isValidDirPath(dirPath)) {
                dirPath = XTools.genValidDirPath(dirPath);
            }
            XTools.mkdir(dirPath);

            return dirPath;
        }

        /// <summary>
        /// 复制执行函数：由于要递归，就传个参数
        /// </summary>
        /// <param name="srcDir">string</param>
        /// <returns>Boolean</returns>
        protected Boolean copy(string srcDir) {
            string file = "";
            string fileName = "";
            string copyInfo = "";
            string desDir = this.getDesDir();
            string [] dirs  = Directory.GetDirectories(srcDir);
            string[] files = Directory.GetFiles(srcDir);
            int loc  = 0;
            for (loc = 0; loc < files.Length; loc ++ ) {
                file = files[loc];
                if(this.inFileFormat(XTools.getFileFormat(file))) {
                    fileName    = XTools.getFileNameHasFormat(file);
                    File.Copy(file, desDir + "\\" + fileName, true);
                    copyInfo = String.Format("[Success]\t{0}", file);
                   this._form.setExecLog(copyInfo);
                }
            }
            //递归复制子目录里的文件
            for (loc = 0; loc < dirs.Length; loc ++ ) {
                this.copy(dirs[loc]);
            }

            return true;
        }

        protected Boolean inFileFormat(string fileFormat) {
            if(this._fileFormats[0] == "*") {
                return true;
            }

            return this._fileFormats.Contains(fileFormat);
        }

        public string getErrorInfo() {
            return this._error.getErrorInfo();
        }

        public void setForm(XXiami form) {
            this._form = form;
        }

        public XXiami getForm() {
            return this._form;
        }

        public void setSrcDir(string srcDir) {
            srcDir = this.testIsIeTempDir(srcDir);
            this._paramPool.Add("srcDir", XTools.genValidDirPath(srcDir));
        }

        public void setDesDir(string desDir) {
            this._paramPool.Add("desDir", XTools.genValidDirPath(desDir));
        }

        public string testIsIeTempDir(string dirPath) {
            Regex dirMode   = new Regex(@"/|\\");
            if(dirMode.IsMatch(dirPath)) {
                return dirPath;
            }

            return this.genIeTempDirPath(dirPath);
        }

        public string getDesDir() {
            return (string)this.getParamPoolElement("desDir");
        }
        
        public string getSrcDir() {
            return (string)this.getParamPoolElement("srcDir");
        }

        public void setFileFormat(string fileFormat) {
            this._fileFormats = fileFormat.Split(XFile.needle, StringSplitOptions.RemoveEmptyEntries);
        }

        protected void setParamPoolElement(string indexName, object elementValue) {
            this._paramPool.Add(indexName, elementValue);
        }
        
        protected object getParamPoolElement(string indexName) {
            if(this._paramPool.Contains(indexName)) {
                return this._paramPool[indexName];
            }

            return null;
        }

    }
}
